﻿using System;
using System.Collections.Generic;

using E.BLL;
using E.Model;
using E.Web.UI.Page;
using E.Web.Common;

using X.Common;

namespace E.Web.Teacher
{
    public partial class Index : TeacherBasePage
    {
        protected override void ShowPage()
        {
            if (ispost)
            {
                string loginid = XRequest.GetString("LoginIDTxt");
                string pwd = XRequest.GetString("PwdTxt");
                //string vcode = XRequest.GetString("validateCodeTxt");

                CheckForm();

                TeacherModel model = new TeacherModel();
                model.LoginID = loginid;
                model.Pwd = Utils.MD5(pwd);

                model = (new TeacherBLL()).Login(model);
                if (model.ID != -1)
                {
                    Utils.WriteCookie("teacherid", model.ID.ToString());
                    Utils.WriteCookie("teachername", model.Name);
                    Utils.WriteCookie("authority", ((int)model.Authority).ToString());
                    IList<DictAreaModel> areaList = GetTeacherAreaList(model.ID);
                    if (areaList.Count > 0)
                    {
                        if (areaList.Count > 1)
                        {
                            ExecuteJS("alert('登录成功'); window.top.location = '/AreaSlt.aspx';");
                        }
                        else
                        {
                            Utils.WriteCookie("areaid", areaList[0].ID.ToString());
                            Utils.WriteCookie("a2", model.A2.ToString());
                            ExecuteJS("alert('登录成功'); window.top.location = '/frames/Index.aspx';");
                        }
                    }
                    else
                    {
                        ExecuteJS("alert('用户名与密码不符'); window.top.location = '/Index.aspx';");
                    }
                }
                else
                {
                    ExecuteJS("alert('用户名与密码不符'); window.top.location = '/Index.aspx';");
                }
            }
        }

        private void CheckForm()
        {
            bool isWrong = false;
            string tips = "";
            if (XRequest.GetString("LoginIDTxt").Trim().Equals("") || XRequest.GetString("PwdTxt").Trim().Equals(""))
            {
                tips = "用户名与密码不符";
                isWrong = true;
            }
            //else if (XRequest.GetString("validateCodeTxt").Trim().Equals("") || XRequest.GetString("validateCodeTxt").Trim().ToLower() != Utils.GetCookie("vcode"))
            //{
            //    tips = "验证码错误";
            //    isWrong = true;
            //}

            if (isWrong)
            {
                ExecuteJS("alert('" + tips + "'); window.top.location = '/Index.aspx';");
                return;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
